//
//  LGStatuesTool.m
//  微博
//
//  Created by MS on 15-12-8.
//  Copyright (c) 2015年 MS. All rights reserved.
//

#import "LGStatuesTool.h"
#import "LGHttpTool.h"
#import "LGAccountTool.h"
#import "LGAccount.h"
#import "LGStatus.h"
#import "LGStatuesParam.h"
#import "LGStatuesResult.h"
#import "LGStatuesCacheTool.h"
@implementation LGStatuesTool

+(void)newStatuesWithSinceID:(NSString *)sinceID success:(void(^)(NSArray * statues))success failure:(void(^)(NSError * error))failure
{
    
    LGStatuesParam * parama = [[LGStatuesParam alloc] init];
    
    if(sinceID){//有微博数据才需要上拉刷新
           parama.since_id = sinceID;
    }
     parama.access_token = [LGAccountTool account].access_token;
    

    NSArray * statues = [LGStatuesCacheTool statuesWithParam:parama];
    
    if (statues.count) {
        if (success) {
            success(statues);
        }
        return;
    }
    
    
    [LGHttpTool GET:@"https://api.weibo.com/2/statuses/friends_timeline.json" parameters:[parama keyValues] success:^(id responseObject) {
        
     LGStatuesResult * result = [LGStatuesResult objectWithKeyValues:responseObject];
                
        //把statues传出去
        if (success) {
            success(result.statuses);
        }
        
        [LGStatuesCacheTool deleteOldData];
        [LGStatuesCacheTool saveDataWithArray:responseObject[@"statuses"]];
        
    } failure:^(NSError *error) {
        
        if (failure) {
            
            failure(error);
        }
    }];

}

+(void)moreStatuesWithMaxID:(NSString *)maxID success:(void(^)(NSArray * statues))success failure:(void(^)(NSError * error))failure
{
    
    LGStatuesParam * parama = [[LGStatuesParam alloc] init];
    
    if(maxID){//有微博数据才需要上拉刷新
        parama.max_id = maxID;
    }
    
    parama.access_token = [LGAccountTool account].access_token;
    
   NSArray * statues = [LGStatuesCacheTool statuesWithParam:parama];
    
    if (statues.count) {
        if (success) {
            success(statues);
        }
        return;
    }
    
    [LGHttpTool GET:@"https://api.weibo.com/2/statuses/friends_timeline.json" parameters:[parama keyValues] success:^(id responseObject) {
        
        LGStatuesResult * result = [LGStatuesResult objectWithKeyValues:responseObject];
        
        //把statues传出去
        if (success) {
            success(result.statuses);
        }
        
        [LGStatuesCacheTool saveDataWithArray:responseObject[@"statuses"]];
        
    } failure:^(NSError *error) {
        
        if (failure) {
            
            failure(error);
        }
    }];

    
}


@end
